﻿using System;
using System.Linq.Expressions;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using System.Web;

namespace Beeldbankmvc.Code
{
    public static class ImageResultHelper
    {
        public static string Image<T>(this UrlHelper helper, Expression<Action<T>> action, int width)
            where T : Controller
        {
            return ImageResultHelper.Image<T>(helper, action, width,  "");
        }

        public static string Image<T>(this UrlHelper helper, Expression<Action<T>> action, int width, string alt)
            where T : Controller
        {

            string url = LinkBuilder.BuildUrlFromExpression<T>(helper.RequestContext, helper.RouteCollection, action);
            return string.Format("<img src=\"{0}\" width=\"{1}\" alt=\"{2}\" />", url, width, alt);
        }

        public static string Image<T>(this UrlHelper helper, Expression<Action<T>> action, int width, string alt, string title, string linkUrl)
              where T : Controller
        {
            string url = LinkBuilder.BuildUrlFromExpression<T>(helper.RequestContext, helper.RouteCollection, action);
            if (String.IsNullOrEmpty(linkUrl))
                return string.Format("<img src=\"{0}\" width=\"{1}\" alt=\"{2}\" title=\"{3}\" />", url.Replace("&", "&amp;"), width, alt, title);
            else
                return string.Format("<a href=\"{0}\"><img src=\"{1}\" width=\"{2}\" alt=\"{3}\" title=\"{4}\" /></a>", linkUrl.Replace("&", "&amp;"), url.Replace("&", "&amp;"), width, alt, title);
        }

        public static string Image<T>(this UrlHelper helper, Expression<Action<T>> action, int width, string alt, string title, string linkUrl, string usemap)
              where T : Controller
        {
            string url = LinkBuilder.BuildUrlFromExpression<T>(helper.RequestContext, helper.RouteCollection, action);
            if (String.IsNullOrEmpty(linkUrl))
                return string.Format("<img src=\"{0}\" width=\"{1}\" alt=\"{2}\" title=\"{3}\" usemap=\"{4}\" />", url.Replace("&", "&amp;"), width, alt, title, usemap);
            else
                return string.Format("<a href=\"{0}\"><img src=\"{1}\" width=\"{2}\" alt=\"{3}\" title=\"{4}\" /></a>", linkUrl.Replace("&", "&amp;"), url.Replace("&", "&amp;"), width, alt, title);
        }

    }

    public class ImageResult : ActionResult
    {
        public ImageResult() { }

        public Image Image { get; set; }
        public ImageFormat ImageFormat { get; set; }
        public bool cacheHeaderToNull { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            // verify properties
            if (Image == null)
            {
                throw new ArgumentNullException("Image");
            }
            if (ImageFormat == null)
            {
                throw new ArgumentNullException("ImageFormat");
            }

            // output
            context.HttpContext.Response.Clear();

            if (ImageFormat.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
            if (ImageFormat.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
            if (ImageFormat.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
            if (ImageFormat.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
            if (ImageFormat.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
            if (ImageFormat.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
            if (ImageFormat.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";

            // ANDRE Nog even bespreken of dit gaat werken voor beheerders
            if (cacheHeaderToNull)
            {
                context.HttpContext.Response.AddHeader("Cache-Control", "max-age=0");
            }
            else
            {
                context.HttpContext.Response.AddHeader("Cache-Control", "max-age=5040"); // one week 60 * 60 * 12 * 7
            }

            Image.Save(context.HttpContext.Response.OutputStream, ImageFormat);
        }
    }
}
